package com.oa.hyperion.api.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;

import com.oa.hyperion.entity.assets.*;
import com.oa.hyperion.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 资产总账controller
 * author yuanchongwei
 * date 2022-1-18
 */
@Api(value="资产总账controller",tags={"资产总账操作接口"})
@RestController
@RequestMapping("/v1/assets")
public class AssetsController {
    @Autowired
    private AssetsGeneralLedgerService assetsService;

    /**
     * 获取资产总账全部信息
     */
    @ApiOperation("所有资产获取")
    @GetMapping("all")
    public List<AssetsGeneralLedger> getAll(){
        return assetsService.getAllAssets();
    }

    /**
     * 分页查询
     */

    @ApiOperation("所有资产分页查询")
    @GetMapping("all/page")
    public PageInfo<AssetsGeneralLedger> getAllByPage(Integer pageNum, Integer pageSize){
        return assetsService.getByCondition(pageNum,pageSize);
    }

    @ApiOperation("资产总账条件查询")
    @GetMapping("by/{conditions}")
    public List<AssetsGeneralLedger> getAssetsByCondition(@PathVariable String conditions){
        // TODO 获取页面提交的查询条件
        // 单条件查询，type,status,belongs_to
        // 多条件查询，type,status
        // 类型转换
        int condition = Integer.parseInt(conditions);

        AssetsGeneralLedger assets = new AssetsGeneralLedger();
        assets.setType(condition);
        assets.setStatus(condition);
        assets.setBelongsTo(condition);
        return assetsService.selectAssets(assets);
    }


    @ApiOperation("资产新增")
    @PostMapping("add")
    public String addAssetsGeneralLedger(@RequestBody AssetsGeneralLedger assets){
        // TODO 判断资产类型，根据类型决定执行的下一步操作
        /**
         * 办公用品类：先判断资产是否完全相同，先判断资产类型为非安装类的（名称、型号、条码、价格、类型），相同则可直接添加库存
         *           报废时，直接修改库存-1
         * 优先级：
         *      1.条码不同新增
         *      2.价格不同新增
         *      3.型号不同新增
         *      4.名称不同新增
         *    如果都相同，可直接修改库存
         *
         * 其它类别，可直接新增到清单中
         */
        if(assets.getType() == 1){
            AssetsGeneralLedger assetsGeneralLedger = assetsService.getAssetsByBarCode(assets.getBarCode());
            if (assetsGeneralLedger == null) {
                assetsService.insert(assets);
                return "添加成功";
            } else if (assetsGeneralLedger.getPrice() != assets.getPrice()) {
                assetsService.insert(assets);
                return "添加成功";
            } else if (!assetsGeneralLedger.getModel().equals(assets.getModel())) {
                assetsService.insert(assets);
                return "添加成功";
            }else if(!assetsGeneralLedger.getName().equals(assets.getName())){
                assetsService.insert(assets);
                return "添加成功";
            }
            else{
                //名称、型号、条码、价格、类型都相同时,在原资产基础上库存+1
                int stockNew = assets.getStock()+assetsGeneralLedger.getStock();
                assetsGeneralLedger.setStock(stockNew);
                if(assetsService.update(assetsGeneralLedger)){
                    return "该办公品用已存在,库存添加成功";
                }
                else{
                    return "库存更新失败，请手动更新";
                }
            }
        }
        //非办公用品类资产，可直接新增
        else{
            if(assetsService.insert(assets)) {
                return "添加成功";
            }
            else{
                return "添加失败";
            }
        }
    }

    @ApiOperation("根据资产类型和状态查询资产信息")
    @GetMapping("typeof/{type}/{status}")
    public List<AssetsGeneralLedger> getGeneralLedgerBy(@PathVariable int type,@PathVariable int status){
        if (type == 0) {
            return null;
        }
        return assetsService.getAssetsByTypeAndStatus(type,status);
    }

    @ApiOperation("根据类型查询资产信息")
    @GetMapping("typeof/{type}")
    public List<AssetsGeneralLedger> getGeneralLedgerByType(@PathVariable int type){
        if (type == 0) {
            return null;
        }
        AssetsGeneralLedger assets = new AssetsGeneralLedger();
        assets.setType(type);
        return assetsService.selectAssets(assets);
    }

    @ApiOperation("根据状态查询资产信息")
    @GetMapping("status/{status}")
    public List<AssetsGeneralLedger> getGeneralLedgerByStatus(@PathVariable int status){
        if (status == 0) {
            return null;
        }
        AssetsGeneralLedger assets = new AssetsGeneralLedger();
        assets.setStatus(status);
        return assetsService.selectAssets(assets);
    }

    @ApiOperation("根据所属部门查询资产信息")
    @GetMapping("dpt/{dpt}")
    public List<AssetsGeneralLedger> getGeneralLedgerByDpt(@PathVariable int dpt){
        if (dpt == 0) {
            return null;
        }
        AssetsGeneralLedger assets = new AssetsGeneralLedger();
        assets.setBelongsTo(dpt);
        return assetsService.selectAssets(assets);
    }


}
